package org.testis.gui.mvc.view.dynamicmap;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.DynamicMapControllerList;
import org.testis.gui.mvc.controller.dynamicmap.LoadDynamicMapController;
import org.testis.gui.mvc.utilities.Validatable;
import org.testis.gui.mvc.utilities.ValidationUtils;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author  Cahit Emir Erel
 */
public class LoadDynamicMapView extends AtofBaseDialog implements
        AbstractViewPanel, Validatable {

    private static final long serialVersionUID = 7034522L;

    public LoadDynamicMapView(java.awt.Frame parent,
            boolean modal) {
        super(parent,
                modal);
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel1 = new java.awt.Panel();
        propertyWindow = new java.awt.TextArea();
        suiteLabel = new javax.swing.JLabel();
        firmwareChoice = new java.awt.Choice();
        openButton = new javax.swing.JButton();
        dutChoice = new java.awt.Choice();
        firmwareLabel = new javax.swing.JLabel();
        exitButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Open Dynamic Map");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        propertyWindow.setEditable(false);

        suiteLabel.setFont(new java.awt.Font("Dialog", 0, 11));
        suiteLabel.setText("Firmwares:");

        firmwareChoice.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                firmwareChoiceSelected(evt);
            }
        });

        openButton.setText("Open Dynamic Map");
        openButton.setEnabled(false);
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        dutChoice.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                dutChoiceSelected(evt);
            }
        });

        firmwareLabel.setFont(new java.awt.Font("Dialog", 0, 11));
        firmwareLabel.setText("Dut Name:");

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
        panel1.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(suiteLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
                            .addComponent(firmwareLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dutChoice, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(firmwareChoice, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)))
                    .addComponent(propertyWindow, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                        .addComponent(exitButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(openButton)))
                .addContainerGap())
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(firmwareLabel)
                    .addComponent(dutChoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(suiteLabel)
                    .addComponent(firmwareChoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(propertyWindow, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(openButton)
                    .addComponent(exitButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        String[] choices = DynamicMapControllerList.newInstance().getLoadDynamicMapController().getDutNames();
        if (choices!=null && choices.length>0) {
            dutChoice.add("");
            for (int i=0;i<choices.length;i++) {
                dutChoice.addItem(choices[i]);
            }
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void firmwareChoiceSelected(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_firmwareChoiceSelected

        DynamicMapControllerList.newInstance().getLoadDynamicMapController().
                operationSelectFirmware(firmwareChoice.getSelectedItem(),
                dutChoice.getSelectedItem());
        if (firmwareChoice.getSelectedItem().equals("")) {
            openButton.setEnabled(false);
        } else {
            openButton.setEnabled(true);
        }
}//GEN-LAST:event_firmwareChoiceSelected

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed

        WindowListener[] listeners = this.getWindowListeners();
        for (int i = 0; i < listeners.length; i++) {
            this.removeWindowListener(listeners[i]);
        }
        this.setEnabled(false);
        DynamicMapControllerList.newInstance().getLoadDynamicMapController().
                operationCloseWindow();
    }//GEN-LAST:event_formWindowClosed

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed

        openButton.setEnabled(false);
        DynamicMapControllerList.newInstance().getLoadDynamicMapController().
                operationLoad();
        this.dispose();
    }//GEN-LAST:event_openButtonActionPerformed

    private void dutChoiceSelected(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_dutChoiceSelected

        DynamicMapControllerList.newInstance().getLoadDynamicMapController().
                operationSelectDut(dutChoice.getSelectedItem());
        String[] choices =
                DynamicMapControllerList.newInstance().getLoadDynamicMapController().
                getFirmwareNames(dutChoice.getSelectedItem());
        firmwareChoice.removeAll();
        openButton.setEnabled(false);
        if (choices != null && choices.length > 0) {
            firmwareChoice.add("");
            for (int i = 0; i < choices.length; i++) {
                firmwareChoice.addItem(choices[i]);
            }
        }

}//GEN-LAST:event_dutChoiceSelected

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed

        this.dispose();
}//GEN-LAST:event_exitButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Choice dutChoice;
    private javax.swing.JButton exitButton;
    private java.awt.Choice firmwareChoice;
    private javax.swing.JLabel firmwareLabel;
    private javax.swing.JButton openButton;
    private java.awt.Panel panel1;
    private java.awt.TextArea propertyWindow;
    private javax.swing.JLabel suiteLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        //System.out.println(this.getClass() + evt.getPropertyName());
        if (evt.getPropertyName().equals(LoadDynamicMapController.SELECT_FIRMWARE)) {
            if (firmwareChoice.getItemCount() == 0) {
                firmwareChoice.addItem(evt.getNewValue().toString());
            }
            firmwareChoice.select(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(LoadDynamicMapController.SHOW_LOADER)) {
            this.centerParent();
        } else if (evt.getPropertyName().equals(LoadDynamicMapController.SHOW_DESCRIPTION)) {
            propertyWindow.setText(evt.getNewValue().toString());
        }
    }

    public boolean validateForm() {
        boolean valid = true;
        StringBuilder builder = new StringBuilder();
        if (ValidationUtils.isEmpty(firmwareChoice.getSelectedItem())) {
            builder.append("Select a Test Suite!\n");
            valid = false;
        }
        if (!valid) {
            JOptionPane.showMessageDialog(null,
                    builder.toString(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        return valid;
    }
}
